﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Ones.Infrastructure.Utils.Pagination.Impl;
using Ones.ServiceCategoryManagement;
using ns = Ones.ServiceCategoryManagement;

namespace Ones.WebAdmin
{
    public partial class CatListCatPage : System.Web.UI.Page
    {
        private ServiceCategoryManagementClient c = null;
        private ns.Category cat = null;
        private int page = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");
            if (Request.Params["page"] != null)
                page = int.Parse(Request.Params["page"]);

            if (Request.Params["keyword"] != null && Request.Params["searchBy"] != null)
            {
                searchPaging(Request.Params["searchBy"].ToString(), Request.Params["keyword"].ToString());
            }
            else
            {
                loadCatePaging();
            }
        }

        protected void catList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");
            string action = e.CommandName.ToString();

            //delete at this page.
            if (action == "delete")
            {
                string itemId = e.CommandArgument.ToString();

                if (c.deleteCategory(itemId))
                {
                    Response.Redirect("~/WebAdmin/CatListCatPage.aspx");
                }
            }
            
            Session.Add("cmd", e.CommandName);
            Session.Add("id", e.CommandArgument);
            //edit at other page.
            Response.Redirect("~/WebAdmin/CatMgmtCatPage.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string searchBy = ddlSearchBy.SelectedValue;
            string keyword = txtSearch.Text;
            Response.Redirect("~/WebAdmin/CatListCatPage.aspx?searchBy=" + searchBy + "&&keyword=" + keyword);
        }

        private void loadCatePaging()
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");
            var lst = c.searchAllCategories();
            c.Close();

            int pageNumber = 1;
            if (Request.QueryString["p"] != null)
            {
                pageNumber = int.Parse(Request.QueryString["p"]);
            }
            LazyPagination<Category> lstPaged = new LazyPagination<Category>(lst.AsQueryable(), pageNumber, 3);

            catList.DataSource = lstPaged;
            catList.DataBind();

            setPager<Category>(lstPaged);
        }

        private void searchPaging(String searchBy, String keyWord)
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");
            var lst = c.searchCategory(searchBy, keyWord);
            c.Close();

            int pageNumber = 1;
            if (Request.QueryString["p"] != null)
            {
                pageNumber = int.Parse(Request.QueryString["p"]);
            }
            LazyPagination<Category> lstPaged = new LazyPagination<Category>(lst.AsQueryable(), pageNumber, 3);

            catList.DataSource = lstPaged;
            catList.DataBind();

            setPager<Category>(lstPaged);
        }

        private void setPager<T>(LazyPagination<T> lstPaged) where T : class
        {
            pager.PageNumber = lstPaged.PageNumber;
            pager.PageSize = lstPaged.PageSize;
            pager.TotalItems = lstPaged.TotalItems;
            pager.FirstItem = lstPaged.FirstItem;
            pager.LastItem = lstPaged.LastItem;
            pager.HasPreviousPage = lstPaged.HasPreviousPage;
            pager.HasNextPage = lstPaged.HasNextPage;
        }
    }
}